import {Message} from 'element-ui'
import Vue from 'vue'
var vue=new Vue()
export default {
  isNull: function (str) {
    str=str+""
    if (str.replace(/(^s*)|(s*$)/g, "").length == 0) {
      return true
    }
    if(str==null || str=="null" || str ==undefined || str =='undefined'){
      return true
    }
    else {
      return false
    }
  },
  // 显示提示信息，第一个提示类别,第二个提示信息，第三个持续时间，第四个是否可关闭
  //例如：this.common.showToast('info','aaa',5000,false) 
  showToast(type='success',msg='成功',duration=3000,showClose='true'){
    vue.$message({
      type: type,
      message: msg,
      showClose:showClose,
      duration:duration
    });
  },
  //相加精度
  accAdd(num1, num2) {
    var r1, r2, m;
    try {
      r1 = num1.toString().split('.')[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = num2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    // return (num1*m+num2*m)/m;  
    return Math.round(num1 * m + num2 * m) / m;
  },
  // 相减精度
  accSub(num1, num2) {
    var r1, r2, m, n;
    try {
      r1 = num1.toString().split('.')[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = num2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return (Math.round(num1 * m - num2 * m) / m).toFixed(n);
  },
  //相乘精度
  formatFloat(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
  },
  //下载文件
  downloadFile(url,loading){
    vue.downloadRequest(url).then(resp=>{
      if(resp && resp.status == 200){
        let headers = resp.headers;
        let blob=new Blob([resp.data],{type: headers['content-type']});
        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        var reg=/\"/g;
        if(headers['content-disposition']){
          const fileName = headers['content-disposition'].replace(reg,"");
          var title = fileName.includes('filename=') ? fileName.split('=')[1]: '下载的表单文件';
          title=decodeURI(title)
          link.download = title;
          link.click();
        }else{
          this.showToast("warning","无权限下载")
        } 
      }else{
        this.showToast("warning","出错了")
      }
    })
  }
}
